import {HTTP} from '../../utils/http'
let http = new HTTP()
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    active: 0,
    phoneLoginshow: false,
    phoneCloseIcon: '/images/icon/close.png',
    value: '',
    currentTime: 60,
    timeCount: '',
    // 发送验证码
    sendBtnShow: true,
    phone: '',
    // 用户信息
    userInfo: [],
    // 是否绑定手机号
    isBindPhone: false,
    // 验证码
    verifyCode: '',
    userId: '',
    token: '',
    workList: [],
    openId: ''
  },

  handleLogin(){
     this.setData({
       show: true
     });
  },

  wxLoginTap() {
    wx.getUserProfile({
      desc: '必须授权才能继续使用', // 必填 声明获取用户个人信息后的用途，后续会展示在弹窗中
      success:(res)=> { 
          console.log('授权成功', res);
          this.setData({ 
              userInfo:res.userInfo,
              show: false,
              isBindPhone: true
          })
          wx.login({
            success: res => {
              if(res.code) {
                // 发起网络请求
                http.request({
                  url: '/ai/draw/login/account/login/wechat',
                  data: {
                    jsCode: res.code
                  },
                  success: (data) => {
                    console.log('登录成功', data);
                    if(!data.data.token) {
                       this.setData({
                          phoneLoginshow: true,
                          openId: data.data.openId
                       })
                    }
                    wx.setStorage({//缓存token
                      data: data.data.token,
                      key: 'token'
                    })
                    wx.reLaunch({
                      url: "/pages/home/home"
                    })
                  }
                })
              }else {
                console.log('登录失败' + res.errMsg)
              }
            }
          })
      },
      fail:(err)=> {
          console.log('授权失败', err);
      }
  })
    
  },

  // 发送验证码
  sendCodeTap() {
    var that = this;
    this.countDown(that, 60);
    http.request({
      url: '/ai/draw/login/send/verify/code',
      data: {
        bindPhone: this.data.phone
      },
      success: (data) => {
        console.log(data);
      }
    })
  },

  countDown(that,count) {
    if (count == 0) {
      that.setData({
        timeCount: '获取验证码',
        sendBtnShow: true
      })
      return;
    }
    that.setData({
      sendBtnShow:false,
      timeCount: count + '秒后重新发送',
    })
    setTimeout(function(){
      count--;
      that.countDown(that, count);
    }, 1000);
  },

  handlePhoneLoginClose() {
    this.setData({
      phoneLoginshow: false
    });
  },

  handlePhoneLogin() {
    this.setData({
      show: false,
      phoneLoginshow: true
    })
  },

  onClose() {
    this.setData({ show: false });
  },

  loginTap() {
    if(this.data.isBindPhone) {
      http.request({
        url: '/ai/draw/login/account/bind/phone',
        data: {
          bindPhone: this.data.phone,
          openId: this.data.openId,
          verifyCode: this.data.verifyCode
        },
        success: (data) => {
          if(data.code === "0") {
            this.setData({
              // userInfo: data.accountInfo,
              userId: res.data.accountInfo.id,
              phoneLoginshow: false,
            })
            Toast.success('绑定成功');
          }
        }
      })
    }else {
      http.request({
        url: '/ai/draw/login/account/login/phone',
        data: {
          bindPhone: this.data.phone,
          verifyCode: this.data.verifyCode
        },
        success: (res) => {
          if(res.code === "0") {
              if (res.code === "0") {
                this.setData({
                  phoneLoginshow: false,
                })
                wx.setStorage({
                  data: res.data.token,
                  key: 'token'
                })
                wx.setStorage({
                  data: res.data.accountInfo.id,
                  key: 'userId'
                })
                wx.reLaunch({
                  url: "/pages/home/home"
                })
              }
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    http.request({
      url: '/ai/draw/account/get/detail',
      data: {
        id: options.userId
      },
      success: (data) => {
        if(data.code === "0") {
            this.setData({
              userInfo: data.data,
              userId: options.userId
            })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  workCreationTap() {
    wx.navigateTo({
      url: '/pages/my-create-work/my-create-work?accountId='+this.data.userId
    })
  },

  workCollectTap() {
    wx.navigateTo({
      url: '/pages/my-collect-work/my-collect-work?accountId='+this.data.userId
    })
  },

  boxCreationTap(){
    wx.navigateTo({
      url: '/pages/my-create-box/my-create-box?accountId='+this.data.userId
    })
  },

  boxCollectTap(){
    wx.navigateTo({
      url: '/pages/my-collect-box/my-collect-box?accountId='+this.data.userId
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  onChange(event) {
    if(event.detail.index === 2) {
      http.request({
        url: '/ai/draw/picture/get/account/page',
        method: 'post',
        data: {
          "type": 2,
          "pageNum": 1,
          "pageSize": 58,
          "accountId": this.data.userId
        },
        success: (res) => {
          if(res.code === "0") {
            this.setData({
              workList: res.data.list
            })
          }
        }
      })
    }
  },

  toAccountList() {
    const accountId = this.data.userId;
    wx.navigateTo({
      url: '/pages/user-list/user-list?accountId='+accountId,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})